package com.itcast.rediscache.bitmap.controller;

import com.itcast.rediscache.bitmap.CacheProvider;
import com.itcast.rediscache.bitmap.UserBitMapRedis;
import com.itcast.rediscache.bitmap.UserSignRecordRedis;
import com.itcast.rediscache.bitmap.domain.Cookie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.Map;
import java.util.TreeMap;

/**
 * https://blog.csdn.net/qq_29994609/article/details/51772394
 * create by zhangtian3 on 2019/7/1
 */
@RestController
@RequestMapping("redisLearning")
public class RedisLearningController {
    @Autowired
    private UserBitMapRedis userBitMapRedis ;

    @Autowired
    private UserSignRecordRedis userSignRecordRedis ;

    @GetMapping(value = "index")
    public String index(){
        String str = "";

        str += CacheProvider.set("tyh", "aaaaaaaaaaaaaaaaaa", 0);
        str += "|";
        str += CacheProvider.get("tyh");
        str += "|";
        str += CacheProvider.del("tyh");

        str += "|||";

        Cookie cookie = new Cookie("aaa", "bbb");
        str += CacheProvider.set("cookie", cookie, 0);
        str += "|";
        str += CacheProvider.get("cookie", Cookie.class);
        str += "|";
        str += CacheProvider.del("cookie");

        return str.toString();
    }

    /**
     * 用户位图映射
     * @return
     */
    @GetMapping(value = "userRedisBit")
    public Integer userRedisBit() {
        userBitMapRedis.userIdToLong("zhangsan") ;
        userBitMapRedis.userIdToLong("lisi") ;
        userBitMapRedis.userIdToLong("wangwu") ;
        userBitMapRedis.userIdToLong("zt971064178") ;
        userBitMapRedis.userIdToLong("水梦星光") ;
        userBitMapRedis.userIdToLong("水梦星光1") ;

        // ---------------- 模拟位运算登录 -------------
        userBitMapRedis.setActiveUserCount("zhangsan");
        userBitMapRedis.setActiveUserCount("lisi");
        userBitMapRedis.setActiveUserCount("wangwu");
        userBitMapRedis.setActiveUserCount("zt971064178");
        userBitMapRedis.setActiveUserCount("水梦星光");
        userBitMapRedis.setActiveUserCount("水梦星光1");

        // ---------------- 统计连续登录用户 -----------
        return userBitMapRedis.getActiveUserCount() ;
    }

    /**
     * 判断用户是否登录
     * @return
     */
    @GetMapping(value = "isLogin/{userId}")
    public Boolean isLogin(@PathVariable(value = "userId") String userId){
        return userBitMapRedis.getCurrentDateActiveStatus(userId) ;
    }

    /**
     * 统计当天登录用户数
     * @return
     */
    @GetMapping(value = "countLoginDays")
    public Long countLoginDays(){
        return userBitMapRedis.countLoginDays() ;
    }

    /**
     * 功能描述: <br>
     * 〈统计7天内的活跃用户数〉
     * @Param: []
     * @Return: java.lang.Integer
     * @Author: zhangtian105
     * @Date: 2020/4/3 15:58
     */
    @GetMapping(value = "getActiveUserCount")
    public Integer getActiveUserCount() {
        return userBitMapRedis.getActiveUserCount() ;
    }

    /**
     * 用户签到测试
     */
    @GetMapping(value = "optUserSign")
    public void optUserSign() {
        LocalDate today = LocalDate.now() ;
        {// do DelSign
            String delRes = userSignRecordRedis.delSignInfo(1000, today) ;
            String delRes1 = userSignRecordRedis.delSignInfo(1000, today.minusDays(1)) ;
            System.out.println("删除签到数据:delRes:"+delRes+",delRes1:"+delRes1);
        }

        { // doSign
            boolean signed = userSignRecordRedis.doSign(1000, today);
            if (signed) {
                System.out.println("您已签到：" + UserSignRecordRedis.formatDate(today, "yyyy-MM-dd"));
            } else {
                System.out.println("签到完成：" + UserSignRecordRedis.formatDate(today, "yyyy-MM-dd"));
            }
        }

        { // checkSign
            boolean signed = userSignRecordRedis.checkSign(1000, today);
            if (signed) {
                System.out.println("您已签到：" + UserSignRecordRedis.formatDate(today, "yyyy-MM-dd"));
            } else {
                System.out.println("尚未签到：" + UserSignRecordRedis.formatDate(today, "yyyy-MM-dd"));
            }
        }

        { // getSignCount
            long count = userSignRecordRedis.getSignCount(1000, today);
            System.out.println("本月签到次数：" + count);
        }

        { // getContinuousSignCount
            long count = userSignRecordRedis.getContinuousSignCount(1000, today);
            System.out.println("连续签到次数：" + count);
        }

        { // getFirstSignDate
            LocalDate date = userSignRecordRedis.getFirstSignDate(1000, today);
            System.out.println("本月首次签到：" + UserSignRecordRedis.formatDate(date, "yyyy-MM-dd"));
        }

        { // getSignInfo
            System.out.println("当月签到情况：");
            Map<String, Boolean> signInfo = new TreeMap<>(userSignRecordRedis.getSignInfo(1000, today));
            for (Map.Entry<String, Boolean> entry : signInfo.entrySet()) {
                System.out.println(entry.getKey() + ": " + (entry.getValue() ? "√" : "-"));
            }
        }
    }
}
